/**
 * 返回所有子路由
 */
const getChildrenRoutes = (routes) => {
  const result = [];
  routes.forEach((route) => {
    if (route.children && route.children.length > 0) {
      result.push(...route.children);
    }
  });
  return result;
};
/**
 * 处理脱离层级的路由：某个一级路由为其他子路由，则剔除该一级路由，保留路由层级
 * @param {*} routes router.getRoutes()
 */
export const filterRouters = (routes) => {
  const childrenRoutes = getChildrenRoutes(routes);
  return routes.filter((route) => {
    return !childrenRoutes.find((childrenRoute) => {
      return childrenRoute.path === route.path;
    });
  });
};

/**
 * 根据 routes 数据，返回对应 menu 规则数组
 */
export function generateMenus(routes) {
  console.log('routes233', routes);
  const result = [];
  //遍历路由表
  routes.forEach((item) => {
    const menuItem = {
      path: item.path,
      meta: item.meta,
      children: []
    };
    if (item.children && item.children.length > 0) {
      menuItem.children = generateMenus(item.children);
    }
    result.push(menuItem);
  });
  return result;
}
